﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Yugi.Client.ViewModel;
using Yugi.Client;

namespace Yugi_Gui.COntrol
{
    /// <summary>
    /// Interaction logic for ListUser.xaml
    /// </summary>
    public partial class ListUser : UserControl
    {
        MainServerHandler main;
        ListOnlineModel model;
        public ListUser(MainServerHandler m)
        {
            InitializeComponent();

            main = m;
            model = main.ListOnline_Model;
            model.listonline.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(listonline_CollectionChanged);
            this.listView1.MouseDoubleClick += new MouseButtonEventHandler(listView1_MouseDoubleClick);
            Init();
        }

        void listView1_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (listView1.SelectedIndex == 0)
                return;
            main.ChatManager_Model.Add(model.listonline[0].name, model.listonline[listView1.SelectedIndex].name);
        }

        void Init()
        {
            main.SendMessageToServer(Common.MSG_GSM.L_User_AllOn, main.External_Model.UserID);
        }

        public void AddCanvas(Canvas can)
        {
            Canvas.SetLeft(this, 0);
            Canvas.SetTop(this, 300);
            can.Children.Add(this);
        }

        void listonline_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
            {
                UserModel user = (UserModel)e.NewItems[e.NewItems.Count - 1];
                this.listView1.Items.Add(user.name);
            }
            if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Remove)
            {
                UserModel user = (UserModel)e.OldItems[0];
                foreach (object ob in listView1.Items)
                {
                    if (user.name == (string)ob)
                    {
                        this.listView1.Items.Remove(ob);
                        break;
                    }
                }
            }
        }


    }
}
